package com.glory.eapequipmentmonitor.activity;

import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.eapequipmentmonitor.R;
import com.glory.eapequipmentmonitor.adapter.PackListAdapter;
import com.glory.eapequipmentmonitor.base.Constant;
import com.glory.eapequipmentmonitor.bean.PackBean;
import com.pda.platform.ui.ui_pdaplatform.base.FreeUI_BaseActivity;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class TakePartActivity extends FreeUI_BaseActivity {


    public PackListAdapter packListAdapter;
    public String packType = "SmallBoxPackageType";
    @BindView(R.id.tvUser)
    TextView tvUser;
    @BindView(R.id.etLotId)
    FreeUI_ClearEditText etLotId;
    @BindView(R.id.rvThingList)
    RecyclerView rvThingList;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.rbSmallBox)
    RadioButton rbSmallBox;
    @BindView(R.id.rbBox)
    RadioButton rbBox;
    @BindView(R.id.checkAll)
    CheckBox checkAll;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_take_part;
    }

    @Override
    protected void initView() {
        tvUser.setText(Constant.CURRENT_USER);

        rvThingList.setLayoutManager(new LinearLayoutManager(this));
        rvThingList.setItemAnimator(new DefaultItemAnimator());
        rvThingList.addItemDecoration(new DividerItemDecoration(this, 1));

        packListAdapter = new PackListAdapter(null);
        packListAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        packListAdapter.isFirstOnly(true);
        rvThingList.setAdapter(packListAdapter);
    }

    @Override
    protected void initData() {
        checkAll.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
                packListAdapter.setAll(isChecked);
            }
        });

        etLotId.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (start == 0 && before == 0 && count > 1) {
                    boolean isHave = false;
                    for (PackBean entity : packListAdapter.getData()) {
                        if (entity.getLOTID().equalsIgnoreCase(s.toString())) {
                            isHave = true;
                            break;
                        }
                    }
                    if (isHave) {
                        showToast("批次号已存在列表中", false);
                    } else {
                        requestLotData(s.toString());
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    private void requestLotData(String lotID) {
        PackBean packBean = new PackBean();
        packBean.setLOTID(lotID);
        packBean.setMAINQTY(3000);
        packBean.setPARTNAME(lotID + "-" + 3000);
        packListAdapter.addData(0, packBean);
        etLotId.requestFocus();
        etLotId.setText("");
    }

    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        freeUI_titleView.setTitle("拆包");
        freeUI_titleView.setRightListener("拆包", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final List<PackBean> selectList = new ArrayList<>();
                for (PackBean packBean : packListAdapter.getData()) {
                    if (packBean.isSelect()) {
                        selectList.add(packBean);
                    }
                }
                if (selectList.size() == 0) {
                    showToast("请选择要拆包的批次", false);
                    return;
                }
//                if (selectList.size() > 10) {
//                    showToast("最多只能选择10条批次", false);
//                    return;
//                }
                packListAdapter.getData().removeAll(selectList);
                packListAdapter.clearPos();
                packListAdapter.notifyDataSetChanged();
                showToast("拆包成功", true);
                checkAll.setChecked(false);
            }
        });
        return new FreeUI_AddViewEntity("header", llMain);
    }

    @OnClick({R.id.rbSmallBox, R.id.rbBox})
    public void onViewClicked(View view) {
        boolean isOk = true;
        if (packListAdapter.getData().size() > 0) {
            isOk = false;
            showToast("列表中存在批次信息，不能更换", false);
        }
        switch (view.getId()) {
            case R.id.rbSmallBox:
                if (isOk) {
                    packType = "SmallBoxPackageType";
                } else {
                    rbBox.setChecked(true);
                }
                break;
            case R.id.rbBox:
                if (isOk) {
                    packType = "BoxPackageType";
                } else {
                    rbSmallBox.setChecked(true);
                }
                break;
        }
    }
}
